package syf.hash;

public class review {
    public static void main(String[] args) {
        Hashtable hashtable=new Hashtable(15);
        hashtable.add(new Employ(15,"syf"));
        hashtable.add(new Employ(30,"wfl"));
        hashtable.add(new Employ(45,"syfwfl"));
        hashtable.showAll();
        System.out.println("======================");
        hashtable.deleteById(1);
        System.out.println("======================");
        hashtable.showAll();
        System.out.println("======================");
    }
}

/**
 * 哈希表
 */
class Hashtable{
    private HashLinkList[] hashLinkLists;
    private int size;

    /**
     * 创教哈希表
     * @param size 哈希表中的数组大小
     */
    public Hashtable(int size) {
        this.size = size;
        hashLinkLists=new HashLinkList[size];
        for (int i=0;i<size;i++){
            hashLinkLists[i]=new HashLinkList();
        }
    }

    /**
     * 添加雇员信息
     * @param employ 被添加的雇员信息
     */
    public void add(Employ employ){
        int num=hashFun(employ.getId());
        hashLinkLists[num].add(employ);
    }

    /**
     * 遍历所有链表
     */
    public void showAll(){
        for (int i=0;i<size;i++){
            hashLinkLists[i].showAll(i+1);
        }
    }
    /**
     *
     */
    public void deleteById(int id){
        int num=hashFun(id);
        hashLinkLists[num].deleteById(id,num+1);
    }
    /**
     * 分组
     * @param id 员工的id号
     * @return 返回所在的组
     */
    public int hashFun(int id){
        return id%size;
    }
}

/**
 * 哈希表下的链表
 */
class HashLinkList{
    private Employ head;
    public void add(Employ employ){
        if (head==null){
            head=employ;
        }else {
            Employ curr=head;
            while (curr.next!=null){
                curr=curr.next;
            }
            curr.next=employ;
        }
    }
    public void showAll(int no){
        if (head==null){
            System.out.println("第"+no+"条链表为空，无法查询");
        }else {
            Employ curr=head;
            System.out.println("第"+no+"条链表包含的数据为：");
            do {
                System.out.println("id为"+curr.getId()+"姓名为"+curr.getName());
                curr=curr.next;
            }while (curr!=null);
        }
    }
    public void deleteById(int id,int no){
        if (head==null){
            System.out.println(no+"链表为空，无法删除");
        }else {
            Employ curr=head;
            //先看头和它相不相等
            if (curr.getId()==id&&curr.next==null){
                head=null;
                System.out.println("从第" + no + "条链表中删除该雇员信息。id：" + curr.getId() + "姓名" + curr.getName());
            }else if (curr.getId()==id&&curr.next!=null){
                head=curr.next;
                System.out.println("从第" + no + "条链表中删除该雇员信息。id：" + curr.getId() + "姓名" + curr.getName());
            }
            else{
                while (true) {
                    if (curr.next!=null&&curr.next.getId() == id ) {
                        System.out.println("从第" + no + "条链表中删除该雇员信息。id：" + curr.next.getId() + "姓名" + curr.next.getName());
                        curr.next = curr.next.next;
                        break;
                    }
                    curr=curr.next;
                    if (curr==null) {
                        System.out.println("没有该雇员信息，无法删除");
                        break;
                    }
                }
            }
        }
    }
}

/**
 * 存放的信息
 */
class Employ{
    private int id;
    private String name;
    Employ next;

    public Employ() {
    }

    public Employ(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
